﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Surface.Presentation;
using Microsoft.Surface.Presentation.Controls;

namespace LetsGoToSchool
{
    /// <summary>
    /// Interaction logic for DoodleImageSelection.xaml
    /// </summary>
    public partial class DoodleImageSelection : UserControl
    {
        private List<RowDefinition> _rows;
        private DoodleImage _selectedImage;
        private Doodle _main;

        public DoodleImageSelection()
        {
            InitializeComponent();

            //load image from database
            _rows = new List<RowDefinition>();
            _selectedImage = this.addImage(null);
            List<Image> images = Database.getAllImages();
            foreach (Image img in images)
            {
                this.addImage(img);
            }
            _imageList.SelectedIndex = 0;
        }

        public void setMain(Doodle doodle)
        {
            _main = doodle;
        }

        /// <summary>
        /// private helper method to add the image to the list box
        /// </summary>
        /// <param name="img"></param>
        /// <returns></returns>
        private DoodleImage addImage(Image img)
        {
            TextBlock text = new TextBlock();
            FrameworkElement element;
            if (img == null)
            {
                element = new Rectangle();
                (element as Rectangle).Fill = new SolidColorBrush(Colors.White);
                text.Text = "No background";
            }
            else
            {
                element = img;
                text.Text = "Type: " + img.Tag;
            }
            element.Width = 250;
            element.Height = 250;
            DoodleImage doodle = new DoodleImage(element, this);

            SurfaceListBoxItem listItem = new SurfaceListBoxItem();
            Contacts.AddPreviewContactTapGestureHandler(listItem, doodle.ImageSelected);

            //create a grid to hold the image and the text
            Grid itemGrid = new Grid();
            itemGrid.Height = 250;
            itemGrid.Width = _imageList.Width - 50;

            itemGrid.Children.Add(doodle.Content);
            doodle.Content.VerticalAlignment = VerticalAlignment.Center;
            doodle.Content.HorizontalAlignment = HorizontalAlignment.Left;

            //set up the text box
            text.FontSize = 36;
            text.FontWeight = FontWeights.Bold;
            text.Width = itemGrid.Width - 300;
            text.TextWrapping = TextWrapping.Wrap;
            text.VerticalAlignment = VerticalAlignment.Center;
            text.HorizontalAlignment = HorizontalAlignment.Right;
            itemGrid.Children.Add(text);

            listItem.Content = itemGrid;
            _imageList.Items.Add(listItem);
            return doodle;
        }

        public void setSelectedImage(DoodleImage doodle) 
        {
            _selectedImage = doodle;
        }

        private void _quitButton_Click(object sender, RoutedEventArgs e)
        {
            _main.Close();
        }

        private void _startButton_Click(object sender, RoutedEventArgs e)
        {
            _main.setBackground(_selectedImage);
            _main.startGame();
            this.Visibility = Visibility.Hidden;
        }
    }

    /// <summary>
    /// a wrapper class around the image to provide the images selected handler
    /// </summary>
    public class DoodleImage 
    {
        private FrameworkElement _image;
        public FrameworkElement Content { get { return _image; } }
        private DoodleImageSelection _selectionBox;
        public ContactEventHandler ImageSelected { get { return delegate{
            _selectionBox.setSelectedImage(this);
        }; } }
        public DoodleImage(FrameworkElement img, DoodleImageSelection dis) 
        {
            _image = img;
            _selectionBox = dis;
        }
    }
}
